<template>
  <div>
    <el-dialog
      title="wordpress账户"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-input
          v-model="kwInput"
          placeholder="输入 任意字符 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
        <el-button
          type="primary"
          class="el-icon-plus add-btn"
          :disabled="listLoading"
          @click="handleAction('create', '添加站点')"
        >添加站点
        </el-button>
      </div>

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >

        <el-table-column align="center" width="100" prop="id" label="id" sortable="custom" />

        <el-table-column align="center" label="网址" prop="domain" sortable="custom" />
        <el-table-column align="center" label="语言" prop="language" sortable="custom" />
        <el-table-column align="center" label="daily post" prop="daily_post" sortable="custom" />
        <el-table-column align="center" label="待爬" prop="amazon_keywords_remain" sortable="custom" />
        <el-table-column align="center" label="佣金账户" prop="amazon_account" sortable="custom" />
        <el-table-column align="center" label="自动博客" prop="amazon_auto_blog" sortable="custom">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.amazon_auto_blog"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleEdit(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,20,50,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />

      <el-dialog
        append-to-body
        :title="title"
        :width="width"
        :visible.sync="innerDialogVisible"
        :close-on-click-modal="false"
      >
        <el-form :model="temp" label-position="left" label-width="160px">

          <el-form-item v-if="action !== 'delete'" label="域名">
            <el-input v-model="temp.domain" />
          </el-form-item>
          <el-form-item v-if="action !== 'delete'" label="语言">
            <el-select v-model="temp.language" placeholder="语言" filterable>
              <el-option
                v-for="item in languages"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="action !== 'delete'" label="username">
            <el-input v-model="temp.username" />
          </el-form-item>

          <el-form-item v-if="action !== 'delete'" label="password">
            <el-input v-model="temp.password" />
          </el-form-item>
          <el-form-item v-if="action !== 'delete'" label="daily_post">
            <el-input v-model="temp.daily_post" />
          </el-form-item>
          <el-form-item v-if="action !== 'delete'" label="佣金账户">
            <el-input v-model="temp.amazon_account" />
          </el-form-item>
          <el-form-item v-if="action !== 'delete'" label="自动博客">
            <el-switch
              v-model="temp.amazon_auto_blog"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="innerDialogVisible = false">取 消</el-button>
          <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/cms/wpaccount'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Wpaccount',
  props: ['webVisible'],
  data() {
    return {
      data: [],
      listLoading: true,
      kwInput: '',
      order_by: 'id',
      order_desc: '1',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '',
      languages: [
        {
          label: '美国英语',
          value: 'en-us'
        },
        {
          label: '日语',
          value: 'ja-jp'
        },
        {
          label: '法语',
          value: 'fr-fr'
        }
      ],
      temp: {
        amazon_auto_blog: true,
        domain: '',
        username: '',
        password: '',
        language: 'en-us',
        daily_post: 400,
        project_id: '',
        amazon_account: ''
      },
      width: '',
      projects: [],
      innerDialogVisible: false
    }
  },
  watch: {
    webVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  methods: {
    resetTemp() {
      this.temp = {
        domain: '',
        username: '',
        password: '',
        project_id: ''
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleEdit(row) {
      this.action = 'edit'
      this.temp = Object.assign({}, row)
      this.confirm()
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.domain}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改站点${row.domain}`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.project_id === '') {
            alert('必须选择项目')
            return
          }
          if (!_data.domain.length) {
            alert('必须填写域名')
            return
          }
          if (!_data.username.length) {
            alert('必须填写用户名')
            return
          }
          if (!_data.password.length) {
            alert('必须填写密码')
            return
          }
          Post({
            domain: _data.domain,
            username: _data.username,
            password: _data.password,
            daily_post: _data.daily_post,
            amazon_auto_blog: _data.amazon_auto_blog,
            amazon_account: _data.amazon_account,
            language: _data.language,
            project_id: _data.project_id

          }
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, _data).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeWebDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin-top: 60px;
    /*margin: 168px auto !important;*/
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
